﻿using UnityEngine;
using System.Text;
using System.Collections.Generic;
using System;

public class InputController : MonoBehaviour ,IInput
{
	public UnityEngine.UI.Text uiLabel;
	private PlatformController controller;

	private IList<string> textList;
    public Space isself = Space.Self;
	void Awake()
	{
		this.textList = new List<string> ();
        PlatformController.Init(gameObject,this);
    }
     
	private string GetText()
	{
		StringBuilder stringBuilder = new StringBuilder ();

		foreach(string text in this.textList)
		{
			stringBuilder.Append(text);
			stringBuilder.Append("\n");
		}

		return stringBuilder.ToString ();
	}

    void IInput.OnBegin(Vector2 pixelPosition)
    {
        this.uiLabel.text = "开始";

        Ray rayl = Camera.main.ScreenPointToRay(new Vector2(pixelPosition.x - 5, pixelPosition.y-5));
        Ray rayr = Camera.main.ScreenPointToRay(new Vector2(pixelPosition.x + 5, pixelPosition.y+5));
        Vector3 pos = Vector3.zero;
        RaycastHit rhl;
        if (Physics.Raycast(rayl, out rhl))
        {
            pos = rhl.point;
        } 
        if (Physics.Raycast(rayr, out rhl))
        {
            pos = rhl.point - pos;
            factor2.x = pos.x / 10;
            factor2.y = pos.z / 10;
        }
    }
    Vector2 factor2 = new Vector2();
    Vector3 prevPos;
    public float factor = 0.05f;
    void IInput.OnMove(Vector2 pixelPosition, Vector2 direction)
    {
        if (this.textList.Count > 10)
        {
            this.textList.RemoveAt(0);
        }
        this.textList.Add("移动：" + direction.x + ":" + direction.y);
        this.uiLabel.text = this.GetText();

        Ray ray = Camera.main.ScreenPointToRay(pixelPosition);
        RaycastHit rh;
        if( Physics.Raycast(ray,out rh) && rh.collider.name == "Plane")
        {
            Vector3 v = rh.point - prevPos;
            v.y = 0;
            //Camera.main.transform.parent.Translate(-v * 0.05f, Space.World );//移动摄像机
            Camera.main.transform.Translate(new Vector3(direction.x * factor2.x, 0, direction.y * factor2.y) *-2, Space.World);//为什么*-2就感觉效果很好，为什么，为什么不是*-1
            //gameObject.transform.position = new Vector3(rh.point.x, gameObject.transform.position.y, rh.point.z);//移动物体  
            prevPos = rh.point;
        }
        this.uiLabel.rectTransform.localPosition = pixelPosition - new Vector2(Screen.width/2,Screen.height/2);
        Debug.Log(direction);
    }

    void IInput.OnScale(Vector2 pixelPos, float distance)
    {
        if (this.textList.Count > 10)
        {
            this.textList.RemoveAt(0);
        }
        this.textList.Add("缩放：" + distance);
        this.uiLabel.text = this.GetText();

        Camera.main.transform.Translate(Camera.main.transform.forward * distance, Space.World);//缩放摄像机
    }

    public void OnEnd(Vector2 pixelPosition)
    {
        this.uiLabel.text = "结束";
    }

    void IInput.OnUpdate()
    { 
    }
}
